package no.ntnu.fp.messages.impl;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.MessageCode;
import no.ntnu.fp.model.Meeting;
import no.ntnu.fp.utils.XmlParser;

public class MeetingUpdatedMessage implements Message {
	private final Meeting meeting;
	
	public MeetingUpdatedMessage(Meeting meeting) {
		this.meeting = meeting;
	}

	public MeetingUpdatedMessage() {
		this(null);
	}
	
	public Meeting getMeeting() {
		return meeting;
	}

	@Override
	public MessageCode getMessageCode() {
		return MessageCode.MEETING_UPDATED_MESSAGE;
	}

	@Override
	public Message fromXmlString(String xml) {
		Document doc = XmlParser.parseXmlString(xml);
		Meeting meeting = new Meeting((Element)doc.getElementsByTagName("meeting").item(0));
		return new MeetingUpdatedMessage(meeting);
	}

	@Override
	public String toString() {
		Document doc = XmlParser.getThreadLocalDocumentBuilder().newDocument();
		Element messageElement = doc.createElement("message");
		messageElement.appendChild(meeting.toXml(doc));
		return getMessageCode() + XmlParser.convertXmlToString(messageElement);
	}
}
